// 1.定义获取token方法
import { Base64 } from "js-base64";
function getToken(): string {
  const token = uni.getStorageSync("userInfo").token || "";
  const base64_token = Base64.encode(token + ":");
  return "Basic " + base64_token;
}


/**
 * 2.定义项目网络请求方法并导出
 * 入参:options
 * 返回值:promise对象
*/
interface optionsType{ 
    url: string;
    method?: "GET" | "POST";
    data?: Object;
}

interface responseType { 
    data: [];
    msg: string;
}

// 定义项目请求基础域名
let baseURL = "https://meituan.thexxdd.cn/api"

export default (options:optionsType):Promise<responseType> => { 
    return new Promise((resolve, reject) => {
        let url = baseURL+options.url 
        uni.request({
            url,
            method:options.method || "GET",
            data: options.data || {},
            header: { Authorization: getToken() },
            success: res=> { 
                // 根据返回的不同状态码进行不同promise状态返回
                let { data, statusCode } = res
                resolve(<responseType>data)
            },
            fail: err => { 
                reject(err)
            }
        })
        
    })
}